//
//  JieSuanViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/9/27.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "JieSuanViewController.h"
#import "DingDanRowTableViewCell.h"
#import "DingDanZhanShiFooterView.h"
#import "DD_shopModel.h"
#import "addressModel.h"
#import "ManageViewController.h"
#import "YuEPayView.h"
#import "DingDanHomeViewController.h"

@interface JieSuanViewController ()<UITableViewDataSource,UITableViewDelegate,MimaViewDelegate>

@end

@implementation JieSuanViewController
{
    
   

    DingDanZhanShiFooterView * _footView;
    UITableView *_table;
    UIView * chongZhi;
    int _h;
    addressModel *_address;
    YuEPayView *_yuEView;
    NSString *_dingDanID;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawNavWithTitle:@"预下单"];
    [self drawBackButton];
    
    self.view.backgroundColor = [UIColor colorWithHue:0.67 saturation:0.02 brightness:0.96 alpha:1.00];
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
    [_table registerClass:[DingDanRowTableViewCell class] forCellReuseIdentifier:@"cell"];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
   
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   [self requestMoRenDiZhi];

    

}
-(void)drawYuEView{
    _yuEView = [[YuEPayView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, KScreenHeight)];
    _yuEView.delegate = self;
    [_yuEView.inputTextField becomeFirstResponder];
    [self.view addSubview:_yuEView];
}


#pragma mark 行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dingDanModel.OrderInforMapper.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 78;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if([WXApi isWXAppInstalled]){
        return 120+150*_kscH;
    }
    return 120+100*_kscH;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 123;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DD_Allmodel *allMoeld = [_dingDanModel.OrderInforMapper objectAtIndex:indexPath.row];
    DD_shopModel * chanPinModel = [allMoeld.orderGoodsrMappers firstObject];
    
    
    DingDanRowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.zheKou.text = @"";
    [cell.image sd_setImageWithURL:[NSURL URLWithString:chanPinModel.goodsImg]];
    cell.title.text = chanPinModel.goodsName;
    cell.num.text = [NSString stringWithFormat:@"X%@",chanPinModel.goodsNumber];
    cell.size.text = chanPinModel.goodsAttr;
    cell.prize.text = [NSString stringWithFormat:@"¥%.2f",[chanPinModel.attr_price floatValue] + [chanPinModel.goodsPrice floatValue]];
    
    return cell;
}

-(void)requestMoRenDiZhi{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [TCUtils showLoadingWithView:self.view];
    [NetWorking NEWjieKouQingQiu:DZ_chaXunDiZhi ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error){
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
            if ([[dic objectForKey:Code]integerValue] == 200) {
                        NSArray *data = [dic objectForKey:Response];
                       if (data.count != 0) {
                        for (NSDictionary *diZhiDic in data) {
                        if ([[diZhiDic objectForKey:@"defaultAddress"]intValue] == 1) {
                            _address = [addressModel objectFromDictionary:diZhiDic];
                               break;
                        }
                    }
                     if (_address == nil) {
                        _address = [addressModel objectFromDictionary:[data firstObject]];
                    }
                }
                 [_table reloadData];
            }
        }
    }];

}

#pragma mark 每组头部试图
-(UIView * )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 78)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel * name = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, KScreenWidth/2, 20)];
    name.font = [UIFont systemFontOfSize:12];
    UILabel * phone = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2, 10, KScreenWidth/2-10, 20)];
    phone.font = [UIFont systemFontOfSize:12];
    phone.textAlignment = NSTextAlignmentRight;
    UILabel * place = [[UILabel alloc]initWithFrame:CGRectMake(10, name.bottom+5, KScreenWidth-10, 20)];
    place.font = [UIFont systemFontOfSize:12];
    UIView * xian = [[UIView alloc]initWithFrame:CGRectMake(0, place.bottom+10, KScreenWidth, 5)];
    xian.backgroundColor = [UIColor cyanColor];
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 70)];
    [button addTarget:self action:@selector(xinXi) forControlEvents:UIControlEventTouchUpInside];
    
    if (_address != nil) {
        name.text = _address.consignee;
        phone.text = _address.mobile;
        place.text = [NSString stringWithFormat:@"%@%@%@%@",_address.province,_address.city,_address.district,_address.address];
        [headerView addSubview:place];
        [headerView addSubview:phone];
        [headerView addSubview:name];

    }else{
        UILabel *xuanZeDiZhiLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,29 ,60 , 20)];
        xuanZeDiZhiLabel.text = @"请选择收货地址";
        xuanZeDiZhiLabel.font = [UIFont systemFontOfSize:13];
        [headerView addSubview:xuanZeDiZhiLabel];
    }
    
    
    [headerView addSubview:xian];
        [headerView addSubview:button];
    return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * bigView = [[UIView alloc]init];
    bigView.backgroundColor =  [UIColor colorWithHue:0.50 saturation:0.00 brightness:0.95 alpha:1.00];
    UIView * peiSong = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
    peiSong.backgroundColor = [UIColor whiteColor];
    [bigView addSubview:peiSong];
    UILabel * fangShi = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth/2, 20)];
    fangShi.text = @"配送方式";
    fangShi.font = [UIFont systemFontOfSize:12];
    [peiSong addSubview:fangShi];
    UILabel * baoYou = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth/2, 5, KScreenWidth/2-10, 20)];
    baoYou.textAlignment = NSTextAlignmentRight;
    baoYou.text = @"包邮";
    baoYou.font = [UIFont systemFontOfSize:12];
    [peiSong addSubview:baoYou];
   chongZhi = [[UIView alloc]init];
    [bigView addSubview:chongZhi];
    chongZhi.backgroundColor=[UIColor whiteColor];
    UILabel * xuanZe = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, KScreenWidth, 20)];
    xuanZe.text = @"选择充值方式";
    xuanZe.font = [UIFont systemFontOfSize:12];
    [chongZhi addSubview:xuanZe];
    if([WXApi isWXAppInstalled]){
        _h = 150*_kscH;
    }else{
        _h = 100*_kscH;
    }
    _footView = [[DingDanZhanShiFooterView alloc]initWithFrame:CGRectMake(0,xuanZe.bottom+10, KScreenHeight, _h)];
    [chongZhi addSubview:_footView];
    UIView * jieSuan = [[UIView alloc]initWithFrame:CGRectMake(0, _footView.bottom+1, KScreenWidth, 50)];
    jieSuan.backgroundColor = [UIColor whiteColor];
    [chongZhi addSubview:jieSuan];
    UIButton * queDing = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 88 * _kscW, 10, 75* _kscW, 30)];
    queDing.backgroundColor = [UIColor redColor];
    [queDing setTitle:@"确定" forState:UIControlStateNormal];
    [queDing setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queDing.titleLabel.font = [UIFont systemFontOfSize:13];
    queDing.layer.cornerRadius = 3;
    [queDing addTarget:self action:@selector(queding) forControlEvents:UIControlEventTouchUpInside];
    [jieSuan addSubview:queDing];
    UILabel * zongJia = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, queDing.left-8, 30)];
    zongJia.textAlignment = NSTextAlignmentRight;
    zongJia.font = [UIFont systemFontOfSize:12];
    NSString *zongJiaStr = [NSString stringWithFormat:@"￥%.2f",[_dingDanModel.totalPrice floatValue]];
    NSMutableAttributedString * abc = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"合计: %@ (包含邮费)",zongJiaStr]];
    [abc addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, abc.length-11)];
    [abc addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(abc.length-6, 6)];
    zongJia.attributedText = abc;
    [jieSuan addSubview:zongJia];
    chongZhi.frame = CGRectMake(0, peiSong.bottom+8, KScreenWidth, jieSuan.bottom+5);
    bigView.frame = CGRectMake(0, 0, KScreenWidth, chongZhi.bottom+10);
    return bigView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
}


-(void)queding{
    [self requestData];
  
}

-(void)requestData{
    NSMutableDictionary *xiaDanDic = [[NSMutableDictionary alloc]init];
    [xiaDanDic setObject:_dingDanModel.orderFatherId forKey:@"orderFatherId"];
    [xiaDanDic setObject:_address.addressId forKey:@"addressId"];
    [xiaDanDic setObject:[self defaultLoadUserName] forKey:@"userName"];
     [xiaDanDic setObject:_address.addressId forKey:@"addressId"];
    [NetWorking NEWjieKouQingQiu:GWC_xiaDan ANDIP:IP And:xiaDanDic anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            if ([[dic objectForKey:Code]intValue] == 200) {
                [self zhiFu];
                _dingDanID = [[dic objectForKey:Response]objectForKey:@"orderFatherId"];;
            }
        }
    }];
 }

-(void)zhiFu{
    if (_footView.zhifuFangShiBJ == 0) {//支付宝
        
    }else if(_footView.zhifuFangShiBJ == 1){//余额
        [self drawYuEView];
        [UIView animateWithDuration:0.5 animations:^{
            _yuEView.alpha = 1.0;
            _yuEView.hidden = NO;
        }];
     }else{//微信
        
    }
}

-(void)queMiMaEnd{
     _yuEView.hidden = YES;
     _yuEView.alpha = 0.0;
     [self requestData];
    //支付接口
    NSMutableDictionary *zhiFuDic = [[NSMutableDictionary alloc]init];
    [zhiFuDic setObject:_dingDanID forKey:@"orderFatherId"];
    [zhiFuDic setObject:_yuEView.inputTextField.text forKey:@"password"];
    [zhiFuDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    
    [NetWorking NEWjieKouQingQiu:DD_yuEZhifu ANDIP:IP And:zhiFuDic anBlock:^(NSDictionary *dic, NSError *error) {
        NSLog(@"%@",dic[Msg]);
        if (!error) {
            if ([[dic objectForKey:Code]intValue] == 200) {
                [TCUtils showAlertViewWithTitle:@"提示" message:@"支付成功" delegate:nil cancel:@"确定" other:nil tag:0];
                DingDanHomeViewController *dingDanView = [[DingDanHomeViewController alloc]init];
                
                [self.navigationController pushViewController:dingDanView animated:YES];
            }
        }

    }];
    
}
#pragma mark 修改信息
-(void)xinXi{
    ManageViewController *diZhiId = [[ManageViewController alloc]init];
    diZhiId.block = ^(addressModel *addre){
        _address = addre;
        [_table reloadData];
    };
    diZhiId.timp = 1;
    [self.navigationController pushViewController:diZhiId animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
